const express = require('express');
const cors = require('cors');
const path = require('path');
require('dotenv').config();

const app = express();

// 中间件
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 路由
app.use('/api/design', require('./routes/design'));
app.use('/api/ppt', require('./routes/ppt'));
app.use('/api/media', require('./routes/media'));
app.use('/api/exercise', require('./routes/exercise'));
app.use('/api/paper', require('./routes/paper'));
app.use('/api/custom', require('./routes/custom'));
app.use('/api/feedback', require('./routes/feedback'));
app.use('/api/resource', require('./routes/resource'));

// 健康检查
app.get('/health', (req, res) => {
  res.json({ status: 'ok', timestamp: new Date().toISOString() });
});

// 错误处理
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({
    code: 500,
    message: '服务器内部错误'
  });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`服务器运行在端口 ${PORT}`);
});

module.exports = app;